//
//  ZXY_MainSplitVC.m
//  NHTraffic
//
//  Created by 宇周 on 15/7/8.
//  Copyright (c) 2015年 duostec. All rights reserved.
//

#import "ZXY_MainSplitVC.h"
NSString *const kKVOCenterVC = @"centerKey";
NSString *const kKVOLeftVC   = @"leftKey";
NSString *const kKVORightVC  = @"rightKey";
NSString *const kKVOCenterV  = @"centerViewKey";

@interface ZXY_MainSplitVC ()
{
    UIView *centerV;
    UIView *leftSplitV;
    UIView *rightSplitV;
    UIView *maskV;
    
    
    Boolean isShowLeft;
    Boolean isShowRight;
    CGRect  screenFrame;
    CGPoint begainPoint;
    
    CGPoint panVelocity;
    
}
@end

@implementation ZXY_MainSplitVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubViews];
    self.canSlideV = YES;
    maskV   = [[UIView alloc] init];
    edgePan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(slideAction:)];
    [self.view addGestureRecognizer:edgePan];
    tapToOrigin = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toOriginAction:)];
    if(self.splitSpace == 0)
    {
        self.splitSpace = 100;
    }
    
    if(self.splitSpaceLeft == 0)
    {
        self.splitSpaceLeft = self.splitSpace;
    }
    
    if(self.splitSpaceRight == 0)
    {
        self.splitSpaceRight = self.splitSpace;
    }
    
    // Do any additional setup after loading the view.
}

- (void)initSubViews
{
    screenFrame       = [UIScreen mainScreen].bounds;
    CGRect leftFrame  = CGRectMake(0, 0, self.splitSpaceLeft, screenFrame.size.height);
    CGRect rightFrame = CGRectMake(screenFrame.size.width - self.splitSpaceRight, 0, self.splitSpaceRight, screenFrame.size.height);

    if(!centerV)
    {
        centerV     = [[UIView alloc] initWithFrame:screenFrame];
        leftSplitV  = [[UIView alloc] initWithFrame:leftFrame];
        rightSplitV = [[UIView alloc] initWithFrame:rightFrame];
        
        [self.view addSubview:leftSplitV];
        [self.view addSubview:rightSplitV];
        [self.view addSubview:centerV];
        self.leftSplitVC.view.frame = leftFrame;
        self.rightSplitVC.view.frame = rightSplitV.bounds;
        [self addChildViewController:self.leftSplitVC];
        [self addChildViewController:self.rightSplitVC];
        [self addChildViewController:self.centerMainVC];
        
        [leftSplitV addSubview:self.leftSplitVC.view];
        [centerV addSubview:self.centerMainVC.view];
        [rightSplitV addSubview:self.rightSplitVC.view];
    }
    else
    {
        centerV.frame     = screenFrame;
        leftSplitV.frame  = leftFrame;
        rightSplitV.frame = rightFrame;
    }
    
    if(self.isShadow)
    {
        centerV.layer.shadowColor = [UIColor blackColor].CGColor;
        centerV.layer.shadowOpacity = 0.8;
        centerV.layer.shadowRadius  = 10;
        centerV.layer.shadowPath    = [UIBezierPath bezierPathWithRect:centerV.frame].CGPath
        ;
        centerV.layer.shadowOffset = CGSizeZero;
        centerV.layer.masksToBounds = NO;
    }
}

- (void)changeCenterV:(UIViewController *)freshCenterVC
{
    [self.centerMainVC.view removeFromSuperview];
    [self.centerMainVC removeFromParentViewController];
    self.centerMainVC = freshCenterVC;
    [self addChildViewController:self.centerMainVC];
    [centerV addSubview:self.centerMainVC.view];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
   
}

- (void)slideAction:(UIPanGestureRecognizer *)leftPan
{
    if(!self.canSlideV)
    {
        return;
    }
    CGPoint location = [leftPan translationInView:self.view];
    CGPoint velocity = [leftPan velocityInView:self.view];
    panVelocity = velocity;
    
    if(centerV.frame.origin.x > 0)
    {
        isShowRight = NO;
        if(!self.canShowLeft)
        {
            centerV.frame = CGRectMake(0, 0, CGRectGetWidth(centerV.frame), CGRectGetHeight(centerV.frame));
            
            return;
        }
    }
    
    if(centerV.frame.origin.x < 0)
    {
        isShowLeft = NO;
        if(!self.canShowRight)
        {
            centerV.frame = CGRectMake(0, 0, CGRectGetWidth(centerV.frame), CGRectGetHeight(centerV.frame));
            return;
        }
    }
    
    if(leftPan.state == UIGestureRecognizerStateBegan)
    {
        begainPoint = centerV.frame.origin;
    }
    else if(leftPan.state == UIGestureRecognizerStateChanged)
    {
        if(location.x > 0)
        {
            if(self.canShowLeft|| isShowRight)
            {
                [self centerViewSlideChanged:location.x + begainPoint.x andVelocity:velocity.x];
            }
        }
        else
        {
            if(self.canShowRight || isShowLeft)
            {
                [self centerViewSlideChanged:location.x + begainPoint.x andVelocity:velocity.x];
            }
        }
        
    }

    else if(leftPan.state == UIGestureRecognizerStateEnded)
    {
        [self centerViewSlideEnd:location.x + begainPoint.x andVelocity:velocity.x];
    }
    else if(leftPan.state == UIGestureRecognizerStateCancelled)
    {
        NSLog(@"Cancel");
    }
    
}

- (void)centerViewSlideEnd:(CGFloat)xoffSet andVelocity:(CGFloat)xVelocity
{
    
    CGFloat offXCenter = centerV.frame.origin.x;
    if(offXCenter > 0 && xVelocity > 0)
    {
        if(offXCenter > self.splitSpaceLeft / 2 || xVelocity > 400)
        {
            if(self.canShowLeft)
            {
                [self toOriginChooseLeftOrRight:YES];
            }
            else
            {
                [self toOriginViewNow];
            }
            
        }
        else
        {
            if(isShowLeft && self.canShowLeft)
            {
                [self toOriginChooseLeftOrRight:YES];
                return;
            }

            [self toOriginViewNow];
        }
    }
    else if (offXCenter > 0 && xVelocity <= 0)
    {
        [self toOriginViewNow];
    }
    else if (offXCenter < 0 && xVelocity >= 0)
    {
        [self toOriginViewNow];
    }
    else if (offXCenter < 0 && xVelocity < 0)
    {
        if(offXCenter < -self.splitSpaceRight / 2 || xVelocity < -400)
        {
            if(self.canShowRight)
            {
                [self toOriginChooseLeftOrRight:NO];
            }
            else
            {
                [self toOriginViewNow];
            }
        }
        else
        {
            if(isShowRight && self.canShowRight)
            {
                [self toOriginChooseLeftOrRight:NO];
                return;
            }

            [self toOriginViewNow];
        }
    }
}

- (void)centerViewSlideChanged:(CGFloat)xoffSet andVelocity:(CGFloat)xVelocity
{
    CGFloat currentXPosition = xoffSet ;
    if (centerV.frame.origin.x  > 0)
    {
        [self willShowLeftView:xoffSet];
        if(isShowRight)
        {
            if(currentXPosition > 0)
            {
                currentXPosition = 0;
                isShowRight = NO;
            }
        }
        else
        {
            if(currentXPosition > (self.splitSpaceLeft))
            {
                currentXPosition = self.splitSpaceLeft;
            }
        }
    }
    else if(centerV.frame.origin.x < 0)
    {
        [self willShowRightView:xoffSet];
        if (isShowLeft)
        {
            if(currentXPosition < 0)
            {
                currentXPosition = 0;
                isShowLeft = NO;
            }
        }
        else
        {
            if(currentXPosition < -self.splitSpaceRight)
            {
                currentXPosition = -self.splitSpaceRight;
            }
        }
    }
    centerV.frame = CGRectMake(currentXPosition, centerV.frame.origin.y, CGRectGetWidth(centerV.frame), CGRectGetHeight(centerV.frame));
    if (centerV.frame.origin.x  > 0)
    {
        [self willShowLeftView:xoffSet];
    }
    else
    {
        [self willShowRightView:xoffSet];
    }
}

- (void)willShowLeftView:(CGFloat)xoffSet
{
    [self.view sendSubviewToBack:rightSplitV];
    leftSplitV.hidden = NO;
    rightSplitV.hidden = YES;
}

- (void)willShowRightView:(CGFloat)xoffSet
{
    [self.view sendSubviewToBack:leftSplitV];
    leftSplitV.hidden = YES;
    rightSplitV.hidden = NO;
}

- (void)didShowLeftView
{
    isShowRight = NO;
    if(self.canShowLeft)
    {
        isShowLeft  = YES;
        [self showMenuViewSetting];
    }
}

- (void)didShowRightView
{
    if(self.canShowRight)
    {
        isShowRight = YES;
        [self showMenuViewSetting];
    }
    isShowLeft  = NO;
    
}

- (void)showMenuViewSetting
{
    [centerV addGestureRecognizer:tapToOrigin];
    maskV.frame = centerV.bounds;
    [centerV addSubview:maskV];
}

- (void)toOriginAction:(UITapGestureRecognizer *)tap
{
    [self toOriginViewNow];
}

- (void)toOriginChooseLeftOrRight:(BOOL) isLeftChoose
{
    __weak ZXY_MainSplitVC * weakSelf = self;
    [UIView animateKeyframesWithDuration:0.3 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeCubic animations:^{
        CGFloat spaceNow = -weakSelf.splitSpaceRight;
        
        if(isLeftChoose)
        {
            spaceNow = weakSelf.splitSpaceLeft;
            [weakSelf willShowLeftView:0];
            [weakSelf didShowLeftView];
        }
        else
        {
            [weakSelf willShowRightView:0];
            [weakSelf didShowRightView];
        }
        centerV.frame = CGRectMake(spaceNow, centerV.frame.origin.y, CGRectGetWidth(centerV.frame), CGRectGetHeight(centerV.frame));
    } completion:^(BOOL finished) {
        
    }];
}

- (void)toOriginViewNow
{
    [UIView animateKeyframesWithDuration:0.3 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeCubic animations:^{
        centerV.frame = CGRectMake(0, centerV.frame.origin.y, CGRectGetWidth(centerV.frame), CGRectGetHeight(centerV.frame));
       
    } completion:^(BOOL finished) {
        isShowRight = NO;
        isShowLeft  = NO;
        [centerV removeGestureRecognizer:tapToOrigin];
        [maskV removeFromSuperview];
    }];

    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
